﻿using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;
using SalesPortal.Activities;
using System.Threading;
using System.Collections.Generic;

namespace SalesPortal.WorkflowConsole
{

    class Program
    {
        /// <summary>
        /// This code creates a WorkflowApplication, 
        /// subscribes to three workflow life-cycle events, 
        /// starts the workflow with a call to Run, and 
        /// then waits for the workflow to complete. 
        /// 
        /// When the workflow completes, the AutoResetEvent is set 
        /// and the host application completes.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //Activity workflow1 = new Workflow1();
            //WorkflowInvoker.Invoke(workflow1);

            AutoResetEvent syncEvent = new AutoResetEvent(false);
            AutoResetEvent idleEvent = new AutoResetEvent(false);

            //WorkflowApplication wfApp =
            //    new WorkflowApplication(new SalesPortal.Activities.SequentialNumberGuessWorkflow());

            var inputs = new Dictionary<string, object>() { { "MaxNumber", 100 } };

            WorkflowApplication wfApp =
                new WorkflowApplication(new SequentialNumberGuessWorkflow(), inputs);

            //wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
            //{
            //    syncEvent.Set();
            //};

            wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
            {
                int Turns = Convert.ToInt32(e.Outputs["Turns"]);
                Console.WriteLine("Congratulations, you guessed the number in {0} turns.", Turns);

                syncEvent.Set();
            };

            wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e)
            {
                Console.WriteLine(e.Reason);
                syncEvent.Set();
            };

            wfApp.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs e)
            {
                Console.WriteLine(e.UnhandledException.ToString());
                return UnhandledExceptionAction.Terminate;
            };


            // Each time the workflow becomes idle waiting for the next guess, 
            // this handler is called and the idleAction AutoResetEvent is set. 
            wfApp.Idle = delegate(WorkflowApplicationIdleEventArgs e)
            {
                idleEvent.Set();
            };

            wfApp.Run();

            //syncEvent.WaitOne();
            // Loop until the workflow completes.
            WaitHandle[] handles = new WaitHandle[] { syncEvent, idleEvent };
            while (WaitHandle.WaitAny(handles) != 0)
            {
                // Gather the user input and resume the bookmark.
                bool validEntry = false;
                while (!validEntry)
                {
                    int Guess;
                    if (!Int32.TryParse(Console.ReadLine(), out Guess))
                    {
                        Console.WriteLine("Please enter an integer.");
                    }
                    else
                    {
                        validEntry = true;
                        wfApp.ResumeBookmark("EnterGuess", Guess);
                    }
                }
            }


        } // static void Main(string[] args)
    }
}
